load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
load("@rules_license//rules:license.bzl", "license")

license(
    name = "license",
    package_name = "ogre-next",
)

licenses(["notice"])

exports_files(["LICENSE"])

expand_template(
    name = "OgreBuildSettings",
    out = "OgreMain/include/OgreBuildSettings.h",
    substitutions = {
        "#cmakedefine OGRE_STATIC_LIB": "/* undef OGRE_STATIC_LIB */",
        "@OGRE_DEBUG_LEVEL_DEBUG@": "3",
        "@OGRE_DEBUG_LEVEL_RELEASE@": "0",
        "#cmakedefine OGRE_BUILD_RENDERSYSTEM_D3D11": "/* undef OGRE_BUILD_RENDERSYSTEM_D3D11 */",
        "#cmakedefine OGRE_BUILD_RENDERSYSTEM_GL3PLUS": "#define OGRE_BUILD_RENDERSYSTEM_GL3PLUS 1",
        # Order matters for the next two substitutions to avoid malformed directives
        "#cmakedefine OGRE_BUILD_RENDERSYSTEM_GLES2": "/* undef OGRE_BUILD_RENDERSYSTEM_GLES2 */",
        "#cmakedefine OGRE_BUILD_RENDERSYSTEM_GLES": "/* undef OGRE_BUILD_RENDERSYSTEM_GLES */",
        "#cmakedefine OGRE_BUILD_RENDERSYSTEM_METAL": "/* undef OGRE_BUILD_RENDERSYSTEM_METAL */",
        "#cmakedefine OGRE_BUILD_RENDERSYSTEM_VULKAN": "/* undef OGRE_BUILD_RENDERSYSTEM_VULKAN */",
        "#cmakedefine OGRE_BUILD_PLUGIN_PFX": "#define OGRE_BUILD_PLUGIN_PFX 1",
        "#cmakedefine OGRE_BUILD_COMPONENT_HLMS_PBS_MOBILE": "/* undef OGRE_BUILD_COMPONENT_HLMS_PBS_MOBILE */",
        "#cmakedefine OGRE_BUILD_COMPONENT_HLMS_UNLIT_MOBILE": "/* undef OGRE_BUILD_COMPONENT_HLMS_UNLIT_MOBILE */",
        "#cmakedefine OGRE_BUILD_COMPONENT_HLMS_PBS": "#define OGRE_BUILD_COMPONENT_HLMS_PBS 1",
        "#cmakedefine OGRE_BUILD_COMPONENT_HLMS_UNLIT": "#define OGRE_BUILD_COMPONENT_HLMS_UNLIT 1",
        "#cmakedefine OGRE_BUILD_COMPONENT_PAGING": "/* undef OGRE_BUILD_COMPONENT_PAGING */",
        "#cmakedefine OGRE_BUILD_COMPONENT_PLANAR_REFLECTIONS": "/* undef OGRE_BUILD_COMPONENT_PLANAR_REFLECTIONS */",
        "#cmakedefine OGRE_BUILD_COMPONENT_SCENE_FORMAT": "/* undef OGRE_BUILD_COMPONENT_SCENE_FORMAT */",
        "#cmakedefine OGRE_BUILD_COMPONENT_MESHLODGENERATOR": "#define OGRE_BUILD_COMPONENT_MESHLODGENERATOR 1",
        "#cmakedefine OGRE_BUILD_COMPONENT_TERRAIN": "/* undef OGRE_BUILD_COMPONENT_TERRAIN */",
        "#cmakedefine OGRE_BUILD_COMPONENT_VOLUME": "/* undef OGRE_BUILD_COMPONENT_VOLUME */",
        "#cmakedefine OGRE_BUILD_COMPONENT_PROPERTY": "/* undef OGRE_BUILD_COMPONENT_PROPERTY */",
        "#cmakedefine OGRE_BUILD_COMPONENT_OVERLAY": "#define OGRE_BUILD_COMPONENT_OVERLAY 1",
        "#cmakedefine OGRE_BUILD_COMPONENT_RTSHADERSYSTEM": "/* undef OGRE_BUILD_COMPONENT_RTSHADERSYSTEM */",
        "#cmakedefine OGRE_CONFIG_LITTLE_ENDIAN": "#define OGRE_CONFIG_LITTLE_ENDIAN 1",
        "#cmakedefine OGRE_CONFIG_BIG_ENDIAN": "/* undef OGRE_CONFIG_BIG_ENDIAN */",
        "#cmakedefine OGRE_CONFIG_UNIX_NO_X11": "#define OGRE_CONFIG_UNIX_NO_X11 1",
        "@OGRE_SET_DOUBLE@": "0",
        "@OGRE_SET_NODE_INHERIT_TRANSFORM@": "0",
        "@OGRE_SET_ALLOCATOR@": "1",
        "@OGRE_SET_CONTAINERS_USE_ALLOCATOR@": "1",
        "@OGRE_SET_STRING_USE_ALLOCATOR@": "0",
        "@OGRE_SET_MEMTRACK_DEBUG@": "0",
        "@OGRE_SET_MEMTRACK_RELEASE@": "0",
        "@OGRE_SET_ASSERT_MODE@": "1",
        "@OGRE_SET_THREADS@": "0",
        "@OGRE_SET_THREAD_PROVIDER@": "0",
        "@OGRE_SET_DISABLE_MESHLOD@": "0",
        "@OGRE_SET_DISABLE_FREEIMAGE@": "0",
        "@OGRE_SET_DISABLE_JSON@": "1",
        "@OGRE_SET_DISABLE_DDS@": "0",
        "@OGRE_SET_DISABLE_PVRTC@": "1",
        "@OGRE_SET_DISABLE_ETC@": "0",
        "@OGRE_SET_DISABLE_STBI@": "1",
        "@OGRE_SET_DISABLE_ASTC@": "1",
        "@OGRE_SET_DISABLE_ZIP@": "1",
        "@OGRE_SET_DISABLE_VIEWPORT_ORIENTATIONMODE@": "1",
        "@OGRE_SET_DISABLE_GLES2_GLSL_OPTIMISER@": "1",
        "@OGRE_SET_DISABLE_GLES2_VAO_SUPPORT@": "1",
        "@OGRE_SET_DISABLE_GL_STATE_CACHE_SUPPORT@": "1",
        "@OGRE_SET_DISABLE_GLES3_SUPPORT@": "1",
        "@OGRE_SET_RENDERDOC_INTEGRATION@": "1",
        "@OGRE_SET_DISABLE_TBB_SCHEDULER@": "0",
        "@OGRE_SET_USE_BOOST@": "0",
        "@OGRE_SET_PROFILING@": "0",
        "@OGRE_SET_PROFILING_EXHAUSTIVE@": "0",
        "@OGRE_SET_DISABLE_QUAD_BUFFER_STEREO@": "1",
        "@OGRE_SET_DISABLE_AMD_AGS@": "0",
        "@OGRE_SET_DISABLE_FINE_LIGHT_MASK_GRANULARITY@": "1",
        "@OGRE_SET_ENABLE_LIGHT_OBB_RESTRAINT@": "0",
        "@OGRE_SET_USE_SIMD@": "1",
        "@OGRE_SET_RESTRICT_ALIASING@": "1",
        "@OGRE_SET_IDSTRING_ALWAYS_READABLE@": "0",
        "#cmakedefine RTSHADER_SYSTEM_BUILD_CORE_SHADERS": "/* undef RTSHADER_SYSTEM_BUILD_CORE_SHADERS */",
        "#cmakedefine RTSHADER_SYSTEM_BUILD_EXT_SHADERS": "/* undef RTSHADER_SYSTEM_BUILD_EXT_SHADERS */",
    },
    template = "CMake/Templates/OgreBuildSettings.h.in",
)

expand_template(
    name = "OgreGL3PlusBuildSettings",
    out = "OgreMain/include/OgreGL3PlusBuildSettings.h",
    substitutions = {
        "#cmakedefine OGRE_GLSUPPORT_USE_EGL_HEADLESS": "#define OGRE_GLSUPPORT_USE_EGL_HEADLESS=1",
        "#cmakedefine OGRE_GLSUPPORT_USE_GLX": "/* undef OGRE_GLSUPPORT_USE_GLX */",
        "#cmakedefine OGRE_GLSUPPORT_USE_WGL": "/* undef OGRE_GLSUPPORT_USE_GLX */",
        "#cmakedefine OGRE_GLSUPPORT_USE_COCOA": "/* undef OGRE_GLSUPPORT_USE_GLX */",
    },
    template = "CMake/Templates/OgreGL3PlusBuildSettings.h.in",
)

main_srcs = glob(
    [
        "OgreMain/src/*.cpp",
        "OgreMain/src/*.inl",
        "OgreMain/src/Animation/*.cpp",
        "OgreMain/src/CommandBuffer/*.cpp",
        "OgreMain/src/Compositor/**/*.cpp",
        "OgreMain/src/Compute/*.cpp",
        "OgreMain/src/Hash/*.cpp",
        "OgreMain/src/Math/Array/*.cpp",
        "OgreMain/src/Math/Array/SSE2/Single/*.cpp",
        "OgreMain/src/Math/Simple/C/*.cpp",
        "OgreMain/src/Vao/*.cpp",
    ],
    exclude = [
        "OgreMain/src/OgreConfigDialogNoOp.cpp",
        "OgreMain/src/OgreFileSystemLayerNoOp.cpp",
        "OgreMain/src/OgrePVRTCCodec.cpp",
        "OgreMain/src/OgreZip.cpp",
        "OgreMain/src/OgreSTBICodec.cpp",
    ],
) + [
    "OgreMain/src/GLX/OgreTimer.cpp",
    "OgreMain/src/GLX/OgreErrorDialog.cpp",
    "OgreMain/src/GLX/OgreFileSystemLayer.cpp",
    "OgreMain/src/Threading/OgreDefaultWorkQueueStandard.cpp",
    "OgreMain/src/Threading/OgreBarrierPThreads.cpp",
    "OgreMain/src/Threading/OgreLightweightMutexPThreads.cpp",
    "OgreMain/src/Threading/OgreThreadsPThreads.cpp",
    "OgreMain/src/Threading/OgreWaitableEvent.cpp",
]

main_hdrs = glob([
    "OgreMain/include/*.h",
    "OgreMain/include/ogrestd/*.h",
    "OgreMain/include/*.inl",
    "OgreMain/include/debugbreak/*.h",
    "OgreMain/include/Hash/*.h",
    "OgreMain/include/Vao/*.h",
    "OgreMain/include/Threading/*.h",
    "OgreMain/include/Animation/*.h",
    "OgreMain/include/Animation/*.inl",
    "OgreMain/include/CommandBuffer/*.h",
    "OgreMain/include/Compositor/**/*.h",
    "OgreMain/include/Compute/*.h",
    "OgreMain/include/Math/Array/*.h",
    "OgreMain/include/Math/Simple/*.h",
    "OgreMain/include/Math/Simple/C/*.h",
    "OgreMain/include/Math/Simple/C/*.inl",
    "OgreMain/include/Math/Array/SSE2/Single/*.h",
    "OgreMain/include/Math/Array/SSE2/Single/*.inl",
]) + [
    "OgreMain/src/OgreImageResampler.h",
    "OgreMain/src/OgrePixelConversions.h",
    "OgreMain/src/OgreSIMDHelper.h",
    "OgreMain/include/OgreBuildSettings.h",
    "OgreMain/include/OgreGL3PlusBuildSettings.h",
    "OgreMain/include/GLX/OgreTimerImp.h",
    "OgreMain/include/GLX/OgreErrorDialogImp.h",
    "OgreMain/include/Emscripten/OgreConfigDialogImp.h",
]

cc_library(
    name = "ogre-main",
    srcs = main_srcs,
    hdrs = main_hdrs,
    copts = [
        "-Wno-implicit-fallthrough",
        "-Wno-deprecated-declarations",
        "-Wno-array-parameter",
        "-Wno-int-to-pointer-cast",
        "-Wno-missing-braces",
        "-Wno-overloaded-virtual",
        "-Wno-switch",
        "-Wno-unused-function",
        "-Wno-unused-variable",
        "-Wno-class-memaccess",
        "-Wno-ignored-attributes",
        "-Wno-unused-but-set-variable",
        "-fexceptions",
    ] + select({
        "@rules_cc//cc/compiler:clang": [
            "-Wno-implicit-function-declaration",
            "-Wno-tautological-overlap-compare",
            "-Wno-string-conversion",
            "-Wno-pointer-bool-conversion",
            "-Wno-pointer-integer-compare",
            "-Wno-dynamic-class-memaccess",
            "-Wno-constant-conversion",
            "-Wno-unknown-warning-option",
        ],
        "//conditions:default": [],
    }),
    defines = [
        "OGRE_DEBUG_MODE=0",
        "OGRE_GCC_VISIBILITY=1",
    ],
    includes = [
        "OgreMain/include",
        "OgreMain/include/Animation",
        "OgreMain/include/CommandBuffer",
        "OgreMain/include/Compositor",
        "OgreMain/include/Hash",
        "OgreMain/include/Math",
        "OgreMain/include/Threading",
    ],
    linkopts = [
        "-ldl",
        "-pthread",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "@egl-registry//:EGL_headers",
        "@freeimage",
        "@libx11",
        "@libxcb",
        "@opengl-registry//:OpenGL_headers",
        "@rapidjson",
    ],
)

ogre_gl_support_hdrs = glob([
    "RenderSystems/GL3Plus/include/*.h",
    "RenderSystems/GL3Plus/include/GL/*.h",
    "RenderSystems/GL3Plus/include/GLSL/*.h",
    "RenderSystems/GL3Plus/include/Vao/*.h",
    "RenderSystems/GL3Plus/include/windowing/*.h",
    "RenderSystems/GL3Plus/include/windowing/EGL/*.h",
    "RenderSystems/GL3Plus/include/windowing/EGL/PBuffer/*.h",
    "RenderSystems/GL3Plus/src/windowing/*.h",
    "RenderSystems/GL3Plus/src/windowing/EGL/*.h",
])

ogre_gl_support_srcs = glob([
    "RenderSystems/GL3Plus/src/windowing/EGL/PBuffer/*.cpp",
    "RenderSystems/GL3Plus/src/windowing/*.cpp",
    "RenderSystems/GL3Plus/src/GLSL/*.cpp",
    "RenderSystems/GL3Plus/src/Vao/*.cpp",
    "RenderSystems/GL3Plus/src/*.cpp",
])

cc_binary(
    name = "RenderSystem_GL3Plus.so",
    srcs = ogre_gl_support_srcs + ogre_gl_support_hdrs,
    copts = [
        "-Wno-implicit-fallthrough",
        "-Wno-deprecated-declarations",
        "-Wno-array-parameter",
        "-Wno-int-to-pointer-cast",
        "-Wno-missing-braces",
        "-Wno-overloaded-virtual",
        "-Wno-switch",
        "-Wno-unused-function",
        "-Wno-unused-variable",
        "-Wno-class-memaccess",
        "-Wno-ignored-attributes",
        "-Wno-unused-but-set-variable",
        "-fexceptions",
    ] + select({
        "@rules_cc//cc/compiler:clang": [
            "-Wno-implicit-function-declaration",
            "-Wno-tautological-overlap-compare",
            "-Wno-string-conversion",
            "-Wno-pointer-bool-conversion",
            "-Wno-pointer-integer-compare",
            "-Wno-dynamic-class-memaccess",
            "-Wno-constant-conversion",
            "-Wno-unknown-warning-option",
        ],
        "//conditions:default": [],
    }),
    includes = [
        "RenderSystems/GL3Plus/include",
        "RenderSystems/GL3Plus/include/GL",
        "RenderSystems/GL3Plus/include/GLSL",
        "RenderSystems/GL3Plus/include/Vao",
        "RenderSystems/GL3Plus/include/windowing",
        "RenderSystems/GL3Plus/include/windowing/EGL",
        "RenderSystems/GL3Plus/src/GLSL/include",
        "RenderSystems/GL3Plus/src/windowing",
    ],
    linkopts = ["-pthread"],
    linkshared = True,
    visibility = ["//visibility:public"],
    deps = [
        ":ogre-main",
        "@opengl-registry//:OpenGL_headers",
    ],
)

cc_library(
    name = "hlms_common",
    srcs = glob([
        "Components/Hlms/Common/src/*.cpp",
    ]),
    hdrs = glob([
        "Components/Hlms/Common/include/*.h",
        "Components/Hlms/Common/include/*.inl",
    ]),
    copts = [
        "-Wno-overloaded-virtual",
        "-Wno-switch",
        "-Wno-unused-variable",
        "-Wno-class-memaccess",
        "-fexceptions",
    ] + select({
        "@rules_cc//cc/compiler:clang": [
            "-Wno-private-header",
            "-Wno-constant-conversion",
            "-Wno-unknown-warning-option",
        ],
        "//conditions:default": [],
    }),
    include_prefix = "Hlms/Common",
    includes = ["Components/Hlms/Common/include"],
    strip_include_prefix = "Components/Hlms/Common/include",
    visibility = ["//visibility:public"],
    deps = [":ogre-main"],
)

cc_library(
    name = "hlms_pbs",
    srcs = glob([
        "Components/Hlms/Pbs/src/*.cpp",
        "Components/Hlms/Pbs/src/*.inc",
        "Components/Hlms/Pbs/src/Cubemaps/*.cpp",
        "Components/Hlms/Pbs/src/InstantRadiosity/*.cpp",
        "Components/Hlms/Pbs/src/IrradianceField/*.cpp",
        "Components/Hlms/Pbs/src/LightProfiles/*.cpp",
        "Components/Hlms/Pbs/src/Vct/*.cpp",
    ]),
    hdrs = glob([
        "Components/Hlms/Pbs/include/*.h",
        "Components/Hlms/Pbs/include/**/*.h",
    ]),
    copts = [
        "-Wno-overloaded-virtual",
        "-Wno-switch",
        "-Wno-unused-variable",
        "-Wno-class-memaccess",
        "-Wno-unused-but-set-variable",
        "-fexceptions",
    ] + select({
        "@rules_cc//cc/compiler:clang": [
            "-Wno-private-header",
            "-Wno-constant-conversion",
            "-Wno-unknown-warning-option",
        ],
        "//conditions:default": [],
    }),
    include_prefix = "Hlms/Pbs",
    includes = ["Components/Hlms/Pbs/include"],
    strip_include_prefix = "Components/Hlms/Pbs/include",
    visibility = ["//visibility:public"],
    deps = [
        ":hlms_common",
        ":ogre-main",
    ],
)

cc_library(
    name = "hlms_unlit",
    srcs = glob([
        "Components/Hlms/Unlit/src/*.cpp",
        "Components/Hlms/Unlit/src/*.inc",
    ]),
    hdrs = glob(["Components/Hlms/Unlit/include/*.h"]),
    copts = [
        "-Wno-overloaded-virtual",
        "-Wno-switch",
        "-Wno-unused-variable",
        "-Wno-class-memaccess",
        "-fexceptions",
    ] + select({
        "@rules_cc//cc/compiler:clang": [
            "-Wno-private-header",
            "-Wno-constant-conversion",
            "-Wno-unknown-warning-option",
        ],
        "//conditions:default": [],
    }),
    include_prefix = "Hlms/Unlit",
    includes = ["Components/Hlms/Unlit/include"],
    strip_include_prefix = "Components/Hlms/Unlit/include",
    visibility = ["//visibility:public"],
    deps = [
        ":hlms_common",
        ":ogre-main",
    ],
)

cc_library(
    name = "overlay",
    srcs = glob(["Components/Overlay/src/*.cpp"]),
    hdrs = glob(["Components/Overlay/include/*.h"]),
    copts = [
        "-Wno-overloaded-virtual",
        "-Wno-switch",
        "-Wno-unused-variable",
        "-fexceptions",
    ] + select({
        "@rules_cc//cc/compiler:clang": [
            "-Wno-private-header",
            "-Wno-constant-conversion",
            "-Wno-unknown-warning-option",
        ],
        "//conditions:default": [],
    }),
    include_prefix = "Overlay",
    includes = ["Components/Overlay/include"],
    strip_include_prefix = "Components/Overlay/include",
    visibility = ["//visibility:public"],
    deps = [
        ":hlms_unlit",
        ":ogre-main",
        "@freetype",
    ],
)
